<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pages extends LG_Controller {

	function  __construct()
	{
		parent::__contruct();
		
		$this->load->library(array('message'));
		$this->load->helper(array('form', 'date', 'language'));
		$this->load->model('page');
		
		// Load language files
		$this->langs->fetch_file('page');
	}
	
	function index()
	{
		$this->list_pages();
	}
	
	function list_pages()
	{
		$condition = array('language' => $this->langs->current_lang);
		$data['pages'] = $this->page->get_all_pages($condition);
		
		//$this->template->assign('langbar', $data, 'langbar');
		//
		//$this->template->assign('content', $data, 'pages_list');
		//$this->template->render();
	}
	
	function view($id)
	{
		$data['page'] = $this->page->get_page($id);
		
		//$this->template->set_template('main');
		//$this->template->assign('langbar', $data, 'langbar');
		$this->template->assign('content', $data, 'page-detail');
		$this->template->render();
	}
	
	// Load sections
	/*function get_sections()
	{
		return $this->section->get_all();
	}*/
	
	// Load categories
	function get_categories()
	{
		$section_id = $this->input->post('page_section');
		return $this->category->get_all($section_id);
	}
	
	function new_page()
	{
		$this->load->library(array('form_validation', 'message'));
		$this->load->model(array('category'));
		
		// Load categories
		$categories = $this->get_categories();
		
		$this->form_validation->set_rules('page_title', lang('page_title'), 'trim|required');
		$this->form_validation->set_rules('page_section', lang('page_section'), 'required');
		$this->form_validation->set_rules('page_category', lang('page_category'), 'required');
		$this->form_validation->set_rules('page_intro', lang('page_intro'), 'trim|required');
		$this->form_validation->set_rules('page_body', lang('page_body'), 'trim|required');
		$this->form_validation->set_rules('page_status', lang('page_status'), 'required');
		
		// Perform validation
		if ($this->input->post('save') || $this->input->post('load_categs'))
		{				
			$categories = $this->category->get_all($this->input->post('page_section'));
		
			if ($this->form_validation->run())
			{
				// Try to save settings
				if ($this->page->save())
				{
					$this->message->set('success', 'Ok, data saved');
				}
				else 
				{
					$this->message->set('error', 'Problem saving data');
				}
				
				redirect('pages/new_page');
			}
			else if (!$this->input->post('load_categs'))
			{
				$this->message->set('error',  validation_errors(), TRUE);
			}
		}
		else if ($this->input->post('cancel'))
		{
			redirect('pages/new_page');
		}
		
		$data['status'] = array('0'=>'Draft', '1'=>'Published'); // languages
		$data['categories'] = $categories;
		
		//$this->template->set_template('main');
		$this->template->add_js(array('jquery', 'common'));
		
		if ($this->request->is_ajax()) 
		{
			if ($this->input->post('page_section'))
			{
				$categories = $this->category->get_all($this->input->post('page_section'));
				
				echo json_encode($categories);
			}
		}
		else
		{
			$this->template->assign('content', $data, 'page-form');
			//$this->template->render();
		}
		
	}
}
